package idv.takeshi.software.productline.bookstore.domain.model.order;

import java.util.Date;

import org.apache.commons.lang.Validate;

import idv.takeshi.software.productline.bookstore.domain.model.shoppingcart.ShoppingCart;
import idv.takeshi.software.productline.bookstore.domain.model.shoppingcart.ShoppingCartItem;

/**
 * A factory for Order.
 * @author takeshi
 *
 */
public class OrderFactory {
	
	/**
	 * New a instance for Order.
	 * @param shoppingCart
	 * @return
	 */
	public Order newInstance(ShoppingCart shoppingCart) {
		Validate.notNull(shoppingCart, "shoppingCart shall always not be null");
		Order order = new Order();
		Date dateTime = null;
		for(ShoppingCartItem item : shoppingCart.getShoppingCartItems()){
			order.addOrderItem(item.getBook(), item.getQuantity());
		}
		dateTime = new Date();
		order.setCreateTime(dateTime);
		order.setUpdateTime(dateTime);
		order.setStatus(OrderStatus.NEW);
		
		return order;
	}
	
	/**
	 * New a instance for an order.
	 * @return
	 */
	public Order newInstance(){
		Order order = new Order();
		Date dateTime = null;
		dateTime = new Date();
		order.setCreateTime(dateTime);
		order.setUpdateTime(dateTime);
		order.setStatus(OrderStatus.NEW);
		
		return order;
	}
	 
}
 
